<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
		
		<el-form-item label="学校" prop="xxid">
		  <el-select v-model="queryParams.xxid" @change="handleQuery" placeholder="请选择学校" :disabled="isXxidFlag" clearable>
		    <el-option
		      v-for="d in dept_self"
		      :key="d.value"
		      :label="d.label"
		      :value="d.value"
		    />
		  </el-select>
      </el-form-item>
      <el-form-item label="规则名称" prop="gzName">
        <el-input
          v-model="queryParams.gzName"
          placeholder="请输入规则名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!-- 		  
      <el-form-item label="开始时间" prop="startTime">
        <el-input
          v-model="queryParams.startTime"
          placeholder="请输入开始时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-input
          v-model="queryParams.endTime"
          placeholder="请输入结束时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
	  
	  <el-form-item label="时间内允许的人数" prop="renshu">
	    <el-input
	      v-model="queryParams.renshu"
	      placeholder="请输入时间内允许的人数"
	      clearable
	      @keyup.enter="handleQuery"
	    />
	  </el-form-item>
	  <el-form-item label="学校id" prop="xxid">
	    <el-input
	      v-model="queryParams.xxid"
	      placeholder="请输入学校id"
	      clearable
	      @keyup.enter="handleQuery"
	    />
	  </el-form-item>
	  -->
	  
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleInit"
        >初始化规则</el-button>
      </el-col>
<!-- 	  
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tc:yuyueguize:edit']"
        >修改</el-button>
      </el-col>
	  <el-col :span="1.5">
	    <el-button
	      type="primary"
	      plain
	      icon="Plus"
	      @click="handleAdd"
	      v-hasPermi="['tc:yuyueguize:add']"
	    >新增</el-button>
	  </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tc:yuyueguize:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['tc:yuyueguize:export']"
        >导出</el-button>
      </el-col>
	  -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="yuyueguizeList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="规则名称" align="center" prop="gzName" />
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" />
      <el-table-column label="时间内允许的人数" align="center" prop="renshu" />
      <!-- <el-table-column label="学校id" align="center" prop="xxid" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['tc:yuyueguize:edit']">修改</el-button>
          <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tc:yuyueguize:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约规则对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="yuyueguizeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="开始时间" prop="startTime">
          <el-input v-model="form.startTime" disabled />
<!-- 		  <el-date-picker
		     v-model="form.startTime"
		     value-format="YYYY-MM-DD hh:mm:ss"
		     type="datetime"
		     range-separator="-"
		     placeholder="开始时间"
			 disabled
		  ></el-date-picker> -->
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-input v-model="form.endTime" disabled />
<!-- 		  <el-date-picker
		     v-model="form.endTime"
		     value-format="YYYY-MM-DD hh:mm:ss"
		     type="datetime"
		     range-separator="-"
		     placeholder="结束时间"
			 disabled
		  ></el-date-picker> -->
        </el-form-item>
<!-- 		<el-form-item label="创建时间" style="width: 308px;">
		   <el-date-picker
		      v-model="dateRange"
		      value-format="YYYY-MM-DD hh:mm:ss"
		      type="datetimerange"
		      range-separator="-"
		      start-placeholder="开始日期"
		      end-placeholder="结束日期"
		   ></el-date-picker>
		</el-form-item> -->
        <el-form-item label="人数" prop="renshu">
          <el-input v-model="form.renshu" type="number" placeholder="时间内允许的人数" />
        </el-form-item>
<!--        <el-form-item label="学校id" prop="xxid">
          <el-input v-model="form.xxid" placeholder="请输入学校id" />
        </el-form-item> -->
        <el-form-item label="规则名称" prop="gzName">
          <el-input v-model="form.gzName" placeholder="请输入规则名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Yuyueguize">
import { listYuyueguize, getYuyueguize, delYuyueguize, addYuyueguize, updateYuyueguize,initYuyueguize } from "@/api/tc/yuyueguize";
import useUserStore from "@/store/modules/user";
import { checkPermi, checkRole } from "@/utils/permission"; // 1. 导入权限判断函数
import { listDept } from "@/api/system/dept";

const userStore = useUserStore();

const { proxy } = getCurrentInstance();

const yuyueguizeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange=ref();
const dept_self = ref([]);
const isXxidFlag=ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 12,
    startTime: null,
    endTime: null,
    renshu: null,
    xxid: null,
    gzName: null
  },
  rules: {
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "结束时间不能为空", trigger: "blur" }
    ],
    renshu: [
      { required: true, message: "时间内允许的人数不能为空", trigger: "blur" }
    ],
    xxid: [
      { required: true, message: "学校id不能为空", trigger: "blur" }
    ],
    gzName: [
      { required: true, message: "规则名称不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

onMounted(() => {
	getDeptInfo();
	
	// if(checkRole(['admin','common'])){
	//   queryParams.value.sbXxid=null;
	// }else{
	//   queryParams.value.sbXxid=userStore.user.deptId;
	// }
})

function getDeptInfo(){
		// console.log(userStore.user)
		if(checkRole(['admin','common'])){
			listDept({}).then(response => {
			  	response.data.forEach(function (element) {
			  	  // 遍历数组，对每个元素进行操作
				   if(element.xxlevel !=null && element.xxlevel != ''){
						dept_self.value.push({"value":element.deptId,"label":element.deptName});
				   }
			  	});
				queryParams.value.xxid=dept_self.value[0].value;
			});
			isXxidFlag.value=false;
		}else{
			dept_self.value.push({"value":userStore.user.deptId,"label":userStore.user.dept.deptName});
			queryParams.value.xxid=dept_self.value[0].value;
			isXxidFlag.value=true;
		}
}

/** 查询预约规则列表 */
function getList() {
  loading.value = true;
  listYuyueguize(queryParams.value).then(response => {
    yuyueguizeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    startTime: null,
    endTime: null,
    renshu: null,
    xxid: null,
    gzName: null
  };
  proxy.resetForm("yuyueguizeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加预约规则";
}
/* 初始化规则 */
function handleInit(){
	// console.log(userStore.user.deptId)
	// console.log(queryParams.value.xxid)
	
	// return;
	
	
	proxy.$modal.confirm('若已存在规则，初始化会重置所有规则！确定要初始化吗？').then(function() {
		  loading.value = true;
		  initYuyueguize(queryParams.value.xxid).then(response => {
		  	// console.log(response)
		  	if(response.code==200){
		  		proxy.$modal.msgSuccess("初始化成功！"+response.data+"条规则！");
				getList();
		  	}else{
		  		proxy.$modal.msgError("初始化失败");
		  	}
		  	loading.value = false;
		  });
	}).then(() => {
	}).catch(() => {});
	
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getYuyueguize(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改预约规则";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["yuyueguizeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateYuyueguize(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addYuyueguize(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除预约规则编号为"' + _ids + '"的数据项？').then(function() {
    return delYuyueguize(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('tc/yuyueguize/export', {
    ...queryParams.value
  }, `yuyueguize_${new Date().getTime()}.xlsx`)
}

getList();
</script>
